<!--
 * @Author: zw 775925302@qq.com
 * @Date: 2023-03-19 20:18:15
 * @LastEditors: zw 775925302@qq.com
 * @LastEditTime: 2023-03-19 22:57:02
-->
<template>
    <ZwTemplateListPage :schema="schema" ref="refListPage" @emitEvents="emitEvents" />

    <ImportTable ref="importRef" @ok="reload()" />

    <EditTable ref="editRef" />
</template>

<script setup lang="ts">

import ImportTable from "../importTable/index.vue"
import EditTable from "../edit/index.vue"
import { useMessage } from "@/hooks/web/useMessage"

import { ref } from "vue"
import { getSchema } from './schema'
import request from '@/config/axios'
import download from '@/utils/download'

const message = useMessage() // 消息弹窗
const refListPage = ref();
const importRef = ref();
const editRef = ref();

const schema = getSchema(refListPage)

const reload = () => {
    refListPage.value.refGird().commitProxy('query')
}

const emitEvents = (params) => {
    if (params.action == "showSelectTable") {
        importRef.value.show()
    } else if (params.action == "showEditTable") {
        editRef.value.show(params.id)
    } else if (params.action == "syncTable") {
        // 基于 DB 同步
        const tableName = params.row.tableName
        message
            .confirm('确认要强制同步' + tableName + '表结构吗?', "温馨提示")
            .then(() => {
                request.put({url:"/infra/codegen/sync-from-db",params:{tableId:params.row.id}}).then(()=>{
                    message.success('同步成功')
                }).catch(()=>{
                    message.error('同步失败')
                })               
            })
    } else if (params.action == "gencode") {
        request.download({ url: '/infra/codegen/download?tableId=' + params.row.id }).then(res=>{
            download.zip(res[0], 'codegen-' + params.row.className + '.zip')
        })
    }
}

</script>